package com.example.controller;

import com.example.model.User;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/thymeleaf")
public class ThymeleafController {

    @GetMapping("/index")
    public String index() {
        System.out.println("跳转 index.html 首页");
        return "tl/index";
    }

    @GetMapping("/user/list")
    public String list(ModelMap modelMap) {
        List<User> userList = new ArrayList<>();
        userList.add(new User("1", "听风", 25, "北京"));
        userList.add(new User("2", "杰克", 23, "上海"));
        userList.add(new User("3", "米亚", 23, "郑州"));

        modelMap.addAttribute("userList", userList);
        return "tl/user/list";
    }

    @GetMapping("/user/map")
    public String map(ModelMap modelMap) {
        Map<String, String> appMap = new HashMap<>();
        appMap.put("A", "IT IS A");
        appMap.put("B", "IT IS B");
        appMap.put("C", "IT IS C");

        modelMap.addAttribute("appMap", appMap);
        return "tl/user/map";
    }

    @GetMapping("/user/info")
    public String info(Model model) {
        List<Map<String, Object>> resultList = new ArrayList<>();
        Map<String, Object> student = new HashMap<String, Object>() {{
            put("sid", "101");
            put("sname", "张三");
            put("sage", "20");
            put("scourse", new HashMap<String, String>() {{
                put("cname", "语文,数学,英语");
                put("cscore", "93,95,98");
            }});
        }};
        resultList.add(student);
        student = new HashMap<String, Object>() {{
            put("sid", "102");
            put("sname", "李四");
            put("sage", "30");
            put("scourse", new HashMap<String, String>() {{
                put("cname", "物理,化学,生物");
                put("cscore", "92,93,97");
            }});
        }};
        resultList.add(student);
        model.addAttribute("resultList", resultList);
        return "tl/user/info";
    }


}
